package org.android.sss;

import java.util.ArrayList;

import org.android.tools.Global;
import org.android.tools.SSSHelper;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class ShoppingListActivity extends Activity  implements OnItemClickListener, AdapterView.OnItemSelectedListener {
	/** Called when the activity is first created. */

	private Integer transport = 0;
	private Integer hours = 0;
	private Button buttonAddToList, buttonSearchProduct, buttonCheckList, buttonSaveList;
	private EditText editTextProductName, editTextHours;
	private Spinner spinnerChooseTransport;
	private String array_spinner[] = null;
	private ListView list;
	private String[] items = {""};
	private ArrayList<String> itemsString = null;
	private AlertDialog alertDialog;
	private int pos;

	private Button buttonShowOnMap;


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shopping_list);
		buttonShowOnMap = (Button) findViewById(R.id.buttonShowOnMap);
		buttonShowOnMap.setEnabled(Global.canShowOnMap == true ? true : false);
		itemsString = new ArrayList<String>();
		buttonAddToList = (Button)findViewById(R.id.buttonAddProductToList);
		buttonSearchProduct = (Button)findViewById(R.id.buttonSearchProductShoppingList);
		buttonCheckList = (Button)findViewById(R.id.buttonCheckShoppingList);
		buttonSaveList = (Button)findViewById(R.id.buttonSaveList);
		
		
		editTextProductName = (EditText)findViewById(R.id.editTextProductName);
		spinnerChooseTransport = (Spinner)findViewById(R.id.spinnerTransport);
		editTextHours = (EditText)findViewById(R.id.editTextHours);

		list = (ListView)findViewById(R.id.listView1);
		list.setOnItemClickListener(this);


		array_spinner=new String[3];
		array_spinner[0] = "walk";
		array_spinner[1] = "bike";
		array_spinner[2] = "car";

		ArrayAdapter adapter = new ArrayAdapter(ShoppingListActivity.this, android.R.layout.simple_spinner_item, array_spinner);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerChooseTransport.setAdapter(adapter);

		buttonCheckList.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String[] tmpList = SSSHelper.checkSavedShoppingList(Global.fb_uid);
				if (tmpList[0].equals("none")) {
					Toast.makeText(ShoppingListActivity.this, "You have not a saved shopping list" ,Toast.LENGTH_SHORT).show();
				}
				else {
					buttonSearchProduct.setEnabled(true);
					itemsString.clear();
					for (int i = 0; i < tmpList.length; i++) {
						itemsString.add(tmpList[i]);
					}
					editTextProductName.setText("");
					refreshList();
				}
				
			}
		});
		
		buttonSaveList.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String tmpShoppingList = "";
				for (int i = 0; i < itemsString.size(); i++) {
					tmpShoppingList += itemsString.get(i) + ";";
				}
				
				int result = SSSHelper.saveShoppingList(Global.fb_uid, tmpShoppingList);
				if (result == 1) {
					Toast.makeText(ShoppingListActivity.this, "The shopping list was saved correctly" ,Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		buttonAddToList.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (editTextProductName.getText().toString().equals("")) {
					Toast.makeText(ShoppingListActivity.this, "You have to type the product name" ,Toast.LENGTH_SHORT).show();
				}
				else {
					buttonSearchProduct.setEnabled(true);
					itemsString.add(editTextProductName.getText().toString().toLowerCase());
					editTextProductName.setText("");
					refreshList();
				}
			}
		});

		buttonSearchProduct.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (editTextHours.getText().toString().equals("")) {
					Toast.makeText(ShoppingListActivity.this, "You have to type how many hours you have to shopping." ,Toast.LENGTH_SHORT).show();
				}
				else {
					hours = Integer.parseInt(editTextHours.getText().toString());

					String tmp = "";
					for (int i = 0; i < itemsString.size(); i++) {
						tmp += itemsString.get(i) + ";";
					}
					String params = Global.xCoordinate.toString()+";"+
							Global.yCoordinate.toString()+";"+
							"Odense;Denmark;"+
							transport.toString()+";"+ 
							hours.toString();
					Global.productsShopsShoppingList = SSSHelper.searchProductAndroid(params,tmp);
					if (Global.productsShopsShoppingList.length == 1 && Global.productsShopsShoppingList[0].idproductShop == 0) {
						if (Global.productsShopsShoppingList[0].product.idproduct == 0) {
							Toast.makeText(ShoppingListActivity.this, "No given products in the database." ,Toast.LENGTH_SHORT).show();
						}
						else {
							Toast.makeText(ShoppingListActivity.this, "There are no shops in the area with the specified purchases. Increase the time of purchase." ,Toast.LENGTH_SHORT).show();
						}
						Global.canShowOnMap = false;
					}
					else {
						Global.canShowOnMap = true;
					}

					buttonShowOnMap.setEnabled(Global.canShowOnMap == true ? true : false);

				}

			}
		});

		buttonShowOnMap.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent myIntent = new Intent(v.getContext(), ShowOnMapActivity.class);
				startActivityForResult(myIntent,0);
			}
		});

		Builder alertDialogBuilder = new Builder(this);
		alertDialogBuilder.setTitle("Remove the product")
		.setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				itemsString.remove(pos);
				if (itemsString.size() == 0) {
					buttonSearchProduct.setEnabled(false);
					buttonShowOnMap.setEnabled(false);
				}
				refreshList();
			}
		}).setNegativeButton("No", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		alertDialog = alertDialogBuilder.create();


	}

	private void refreshList() {
		items = new String[itemsString.size()];
		for (int i = 0; i < itemsString.size(); i++) {
			items[i] = itemsString.get(i);
		}
		list.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,items));
	}
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		pos = arg2;
		alertDialog.setMessage("Do you want to remove "+itemsString.get(pos)+" from the shopping list?");
		alertDialog.show();
		//
	}
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		transport = arg2;

	}
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}
}
